const _ = require("../import");


/**
 * @api {post} /home/index  index
 * @apiDescription  获取首页商品列表，默认返回20条数据（不需要参数，传空对象 {} 即可 ）
 * @apiName index
 * @apiGroup Home
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "请求失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/home/index
 * @apiVersion 1.0.0
 */
_.router.post('/index', async function(req, res, next) {

  console.log("index")
  var data = await _.db.query({
      sql: _.config.sql.getIndex,
      data: []
  })

  res.json({
    status: 200,
    message: '请求成功',
    result: data
  });
});


/**
 * @api {post} /home/info  info
 * @apiDescription 获取商品的详情接口
 * @apiName info
 * @apiGroup Home
 * @apiParam {number} id 商品的id参数，从 /home/index 接口中获得 wares_id 作为id参数传入即可
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "请求失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/home/info
 * @apiVersion 1.0.0
 */
_.router.post('/info', async function(req, res, next) {
  var id = req.body.id;

  var data = (await _.db.query({
    sql: _.config.sql.getInfo,
    data: [id]
  }))[0]

  res.json({
    status: 200,
    message: '请求成功',
    result: data
  });
});


/**
 * @api {post} /home/activity  activity
 * @apiDescription 获取首页轮播下活动分类商品的接口
 * @apiName activity
 * @apiGroup Home
 * @apiParam {number} id 活动的id，取值对应为：1->限时白嫖，2->新品推荐，3->低价高配，4->超值特卖
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "请求失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/home/activity
 * @apiVersion 1.0.0
 */
_.router.post('/activity', async function(req, res, next) {
  var activity_id = req.body.id;

  var data = await _.db.query({
    sql: _.config.sql.getActivity,
    data: [ activity_id ]
  })

  res.json({
    status: 200,
    message: '请求成功',
    result: data
  });
});


/**
 * @api {post} /home/search  search
 * @apiDescription 搜索接口，用于首页搜索商品
 * @apiName search
 * @apiGroup Home
 * @apiParam {string} keyword 搜索关键词，例如：小米
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "请求失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/home/search
 * @apiVersion 1.0.0
 */
_.router.post('/search', async function(req, res, next) {
  var keyword = req.body.keyword;
  console.log("keyword： ", keyword)

  var data = await _.db.query({
    sql: `SELECT * from wares where wares_title LIKE "%${keyword}%"`,
    data: [ ]
  });

  res.json({
    status: 200,
    message: '请求成功',
    result: data
  });

});

module.exports = _.router;

